<?php
include dirname(dirname(__FILE__)).'/admin-verify.php';
include dirname(__FILE__).'/functions.php';
include_once dirname(dirname(dirname(__FILE__))).'/include/host-base.php';
$goodsName=addslashes($_GET["goodsName"]);
$row=getGoodsInfoByName($goodsName);
?>

<html>
	<head>
		<?php include dirname(dirname(dirname(__FILE__))).'/include/common-header.php'; ?>
		<script src="../../js/jquery.ui.widget.js"></script>
		<script src="../../js/jquery.iframe-transport.js"></script>
		<script src="../../js/jquery.fileupload.js"></script>
		<script type="text/javascript">
		

		$(document).ready(function(){


			$("#image").fileupload({
			    url:"ajax/image-upload.php",//文件上传地址，当然也可以直接写在input的data-url属性内
			    formData:{},//如果需要额外添加参数可以在这里添加
			    done:function(e,result){
			        //done方法就是上传完毕的回调函数，其他回调函数可以自行查看api
			        //注意result要和jquery的ajax的data参数区分，这个对象包含了整个请求信息
			        //返回的数据在result.result中，假设我们服务器返回了一个json对象
			    	var json=JSON.parse(result.result);
			    	 if(typeof(json.error) != 'undefined'){
		                    if(json.error != ''){
		                        alert(json.error);
		                    }else{
		                    	$("#url").attr("value",json.url);
		                    	$("#store-name").attr("value",json.storeName);
			                    $("#upload-div").hide();
			                    $("#image-show").attr("src",json.url);
								$("#upload-show").show();    
		                    }
		                }
			    }
			})
			


		
				
			$("input").not("#store-name").not("#pre-store-name").attr("maxlength","16");
			$("#name").blur(function(){

				var _goodsname=$.trim($(this).val());
				var _preGoodsName=$.trim($("#pre-goods-name").val());
				if(_goodsname==null||_goodsname=='')return;
				if(_goodsname.length<2){

					$("#name").parent().parent().addClass("has-error");
					$("#name").next().html("商品名称长度应大于2").fadeIn();
					$("#add-goods-btn").attr("disabled","disabled");

					
					}
				else{
				$.ajax({
					dataType:"text",
					url:'ajax/is-goodsname-exists.php',
					data:{goodsname:_goodsname},
					success:function(data) {
						if(data=='yes'&&_goodsname!=_preGoodsName) {
							$("#name").parent().parent().addClass("has-error");
							$("#name").next().html("此商品已存在").fadeIn();
							$("#add-goods-btn").attr("disabled","disabled");
						} else {
							$("#name").parent().parent().removeClass("has-error")
							$("#name").next().hide();
							$("#add-goods-btn").removeAttr("disabled");
						}
					},
				});

				}
				
				});


			$("#price").blur(function(){

				var _price = $.trim($("#price").val())

				if(isNaN(_price)){

					$("#price").parent().parent().addClass("has-error");
					$("#price").next().html("请输入数字").fadeIn();
					$("#price").val("");
					}
				else{
						if(_price<=0){
							$("#price").parent().parent().addClass("has-error");
							$("#price").next().html("数字应该大于0").fadeIn();
							$("#price").val("");
							}
						else{
							$("#price").parent().parent().removeClass("has-error");
							$("#price").next().hide();
						
							
							}
					
					}
				
		
				});



			$("#pieces-price").blur(function(){

				var _piecePrice = $.trim($("#pieces-price").val())

				if(isNaN(_piecePrice)){	

					$("#pieces-price").parent().parent().addClass("has-error");
					$("#pieces-price").next().html("请输入数字").fadeIn();
					$("#pieces-price").val("");
					}
				else{
						if(_piecePrice<=0){
							$("#pieces-price").parent().parent().addClass("has-error");
							$("#pieces-price").next().html("数字应该大于0").fadeIn();
							$("#pieces-price").val("");
							}
						else{
							$("#pieces-price").parent().parent().removeClass("has-error");
							$("#pieces-price").next().hide();
							}
					
					}
				
		
				});

			$("#number").blur(function(){

				var _number = $.trim($("#number").val())

				if(isNaN(_number)){

					$("#number").parent().parent().addClass("has-error");
					$("#number").next().html("请输入数字").fadeIn();
					$("#number").val("");
					}
				else{
						if(_number<=0){
							$("#number").parent().parent().addClass("has-error");
							$("#number").next().html("数字应该大于0").fadeIn();
							$("#number").val("");
							}
						else{
							$("#number").parent().parent().removeClass("has-error");
							$("#number").next().hide();
							}
					
					}
				
		
				});

			
			
			});
	
	
		</script>
		<title>商品编辑</title>
	</head>
	<body>
	
		<div>
		
		
		<div class="form-horizontal" id="upload-div">
				 
		<div class="form-group" >
				    <label for="image" class="col-sm-2 control-label">图片修改</label>
				    <div class="col-sm-3">
				      <input  type="file" name="image" class="form-control normal-input" id="image">
				    </div>
		</div>	
		
	
				
				 
	</div>
	<div class="form-horizontal" id="upload-show" >
				 
		<div class="form-group" >
				    <label for="image-show" class="col-sm-2 control-label">图片预览</label>
				    <div class="col-sm-3">
				    <img alt="" src= <?php echo '"'.$row['url'].'"'; ?> id="image-show" class="img-thumbnail"/>
				     
				    </div>
		</div>	
		
				
				 
	</div>
	 
		  
		  
		   
			  
	<form class="form-horizontal" role="form" method="post" action="update-goods.php">
				 
		
				  <div class="form-group ">
				    <label for="name" class="col-sm-2 control-label">商品名称</label>
				    <div class="col-sm-5">
				      <input required type="text" name="name" class="form-control normal-input"  value=<?php echo '"'.$row['name'].'"'; ?> id="name" placeholder="商品名称">
				      <p class="help-block" style="display:none"></p>
				    </div>
				  </div>
				  
				  <div class="form-group">
				    <label for="price" class="col-sm-2 control-label">每箱单价</label>
				    <div class="col-sm-5">
				      <input required type="text" name="price" class="form-control normal-input" id="price" value=<?php echo '"'.$row['price'].'"'; ?> placeholder="每箱单价">
						<p class="help-block" style="display:none"></p>
				      </div>
				    
				  </div>
				  
				   <div class="form-group">
				    <label for="pieces-price" class="col-sm-2 control-label">每件单价</label>
				    <div class="col-sm-5">
				      <input required type="text" name="pieces-price" class="form-control normal-input" value=<?php echo '"'.$row['piece_price'].'"'; ?> id="pieces-price" placeholder="每件单价">
				    <p class="help-block" style="display:none"></p>
				    </div>
				  </div>
				  
				    <div class="form-group">
				    <label for="number" class="col-sm-2 control-label">每箱数量</label>
				    <div class="col-sm-5">
				      <input required type="number" name="number" class="form-control normal-input" id="number" value=<?php echo '"'.$row['number'].'"'; ?> placeholder="每箱数量">
				    </div>
				  </div>
				  
				   <div class="form-group">
				    <label for="describe" class="col-sm-2 control-label">商品描述</label>
				    <div class="col-sm-5">
				      <input required type="text" name="describe" class="form-control normal-input" id="describe" value=<?php echo '"'.$row['goods_describe'].'"'; ?> placeholder="商品描述">
				    </div>
				  </div>
				  
				  
		
				  
				   <div class="form-group">
				    <label for="goods-class" class="col-sm-2 control-label">商品种类</label>
				    <div class="col-sm-5">
				    <select class="form-control" id="goods-class" name="goods-class">
				    	 <?php 
		  					$items=getGoodsClass();
		  						foreach ($items as $item)
		  						{
		  							if ($item['name']==$row['class_name']) {
											
											echo '<option selected value='.$item['name'].'>'.$item['name'].'</option>';
		  							}
		  							else {
		  							echo '<option value='.$item['name'].'>'.$item['name'].'</option>';
		  							}
		  						}
		  				?>
					</select>
				    </div>
				  </div>
				  
				 
				 <input  type="text" name="pre-store-name"  id="pre-store-name" value=<?php echo '"'.$row['store_name'].'"'; ?>  style="display: none"/>
				 <input  type="text" name="store-name" id="store-name" value="none" style="display: none" />
				  <input type="text" name="pre-goods-name" id="pre-goods-name" value=<?php echo '"'.$row['name'].'"'; ?> style="display: none"/>
				  <div class="form-group">
				    <div class="col-sm-offset-2 col-sm-10">
				      <button type="submit" class="btn btn-primary" id="add-goods-btn">确认修改</button>
				    </div>
				  </div>
				  
				</form>
			
			
		</div>
		
	</body>
</html>